package com.senior.english.controller;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.senior.english.config.Constant;
import com.senior.english.config.LoadConfiguration;
import com.senior.english.model.TestModel;
import com.senior.english.servicesImp.QuestionaireService;

@Controller
public class QuestionaireController {

	QuestionaireService questionaireService = (QuestionaireService) LoadConfiguration
			.loadJDBC().getBean("questionaireDAO");

	@RequestMapping(value = "/questionlist", method = RequestMethod.GET)
	public String getUsers(ModelMap model) {
		model.addAttribute("listQuestion", questionaireService.listQuestion());
		return "questionlist";
	}
	
	@RequestMapping(value = "/insertquestion", method = RequestMethod.GET)
	public String displayCreateTest(ModelMap model) {
		return "insertquestion";
	}
	@RequestMapping(value = "/insertquestion", method = RequestMethod.POST)
	public String processCreateTest(ModelMap model, HttpServletRequest req) {
		System.out.println("!@#$%^&: " + req.getParameter("test").toString());
		questionaireService.createTest(req.getParameter("test").toString());
		return "insertquestion";
	}
	/**
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toeictest", method = RequestMethod.GET)
	public String getImageQuestions(ModelMap model) {
		model.addAttribute("listImgQuestion", questionaireService.listImgQuestion());
		return "toeictest";
	}
	/**
	 * display uploadimage page
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/uploadimage", method = RequestMethod.GET)
	public String displayUploadImage(ModelMap model) {
		return "uploadimage";
	}

	/**
	 * process uploadimage page
	 * 
	 * @param request
	 * @param fileUpload
	 * @return
	 */
	@RequestMapping(value = "/uploadimage", method = RequestMethod.POST)
	public String processUploadImage(HttpServletRequest request,
			@RequestParam CommonsMultipartFile[] fileUpload) {
		if (fileUpload != null && fileUpload.length > 0) {
			for (CommonsMultipartFile aFile : fileUpload) {

				if (!aFile.getOriginalFilename().equals("")) {
					try {

						aFile.transferTo(new File(Constant.serverImgDirectory
								+ aFile.getOriginalFilename()));
						questionaireService.insertImage(
								Constant.serverImgDirectory,
								aFile.getOriginalFilename());
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return "uploadsuccess";
	}

	/**
	 * load image from database down to server
	 * 
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 */
	@RequestMapping(value = "/loadimage", method = RequestMethod.GET)
	public String loadimage(ModelMap model) throws IOException, SQLException {
		questionaireService.loadImage();
		return "loadimage";
	}

	/**
	 * display uploadaudio page
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/uploadaudio", method = RequestMethod.GET)
	public String displayUploadAudio(ModelMap model) {
		return "uploadaudio";
	}

	/**
	 * process uploadaudio page
	 * 
	 * @param test
	 * @return
	 */
	@RequestMapping(value = "/uploadaudio", method = RequestMethod.POST)
	public String processUploadAudio(HttpServletRequest request,
			@RequestParam CommonsMultipartFile[] fileUpload) {
		if (fileUpload != null && fileUpload.length > 0) {
			for (CommonsMultipartFile aFile : fileUpload) {

				if (!aFile.getOriginalFilename().equals("")) {
					try {

						aFile.transferTo(new File(Constant.serverAudDirectory
								+ aFile.getOriginalFilename()));
						questionaireService.insertAudio(
								Constant.serverAudDirectory,
								aFile.getOriginalFilename());
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return "uploadsuccess";
	}

	/**
	 * load audio down to server
	 * 
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 */
	@RequestMapping(value = "/loadaudio", method = RequestMethod.GET)
	public String loadAudio(ModelMap model) throws IOException, SQLException {
		questionaireService.loadAudio();
		return "loadaudio";
	}

}
